<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com> 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\CaseModel;
use think\Db;

/**
 * Class NavController 导航类别管理控制器
 * @package app\admin\controller
 */
class CaseController extends AdminBaseController
{


	/**
	 * 案例列表
	 * @return [type] [description]
	 */
	public function index()
	{

		$result = Db::name('company')->order("id desc")->paginate(8);
		$this->assign('page',$result->render());
		$this->assign('result',$result);

		return $this->fetch();
	}

	/**
	 * 案列添加页面
	 */
	public function add()
	{
		return $this->fetch();
	}


	/**
	 * 案例添加
	 */
	public function addPost()
	{
		//post 数据
		$post = $this->request->param();
		$data = $post['post'];
		if($data['title'][0])
		{
			foreach($data['title'] as $k=>$v)
			{
				$param[$k]['title'] = $v;
				$param[$k]['content'] = $data['content'][$k];
			}
			$data['param'] = json_encode($param);
		}
		unset($data['title']);unset($data['content']);
		$file = request()->file('user_img');
		if($file){
	        $info = $file->move(STATIC_ROOT . 'effect/upload_xiaoguotu');
	        if($info){    
	            $file_name =  $info->getSaveName();     
	            $data['user_img']   = $file_name;                     			
	        }else{
	            // 上传失败获取错误信息
	            $this->success("添加失败");
	        }
    	}   
		$data['add_time']    = time();
		Db::name('company')->insert($data);
       	$this->success("添加成功！", url("case/index"));

	}

	/**
	 *案例修改页面
	 */
	public function edit()
	{
		$id = $this->request->param('id');

		$data = Db::name('company')->where(['id'=>$id])->find();

		$param = json_decode($data['param'],true);
		$this->assign('param',$param);
		$this->assign('data',$data);
		$this->assign('id',$id);
		return $this->fetch();
	}

	/**
	 * 案例修改
	 */
	public function editPost()
	{
		$post = $this->request->param();
		$data = $post['post'];
		if($data['title'][0])
		{
			foreach($data['title'] as $k=>$v)
			{
				$param[$k]['title'] = $v;
				$param[$k]['content'] = $data['content'][$k];
			}
			$data['param'] = json_encode($param);
		}
		unset($data['title']);unset($data['content']);
		$file = request()->file('user_img');
		if($file){

	        $info = $file->move(STATIC_ROOT . 'effect/upload_xiaoguotu');
	        if($info){    
	        	//删除以前的照片
	        	$list = Db::name('company')->where(['id'=>$data['id']])->find();
	        	$before_img_path = STATIC_ROOT .  'effect/upload_xiaoguotu/'.$list['user_img'];
	        	if(is_file($before_img_path))
	        	{
	        		unlink($before_img_path);
	        	}
	            $file_name =  $info->getSaveName(); 
	            $data['user_img'] = $file_name;                         			
	        }else{
	            // 上传失败获取错误信息
	            $this->success("修改失败");
	        }
    	}

		$id = $data['id']; unset($data['id']);
		
		Db::name('company')->where(['id'=>$id])->update($data);
		$this->success("修改成功!", url("case/index"));

	}

	/**
	 * 删除
	 */
	public function delete()
	{
		$id = $this->request->param('id');
		Db::name('company')->where(['id'=>$id])->delete();
		$this->success("删除成功!", url("case/index"));
	}


	/**
	 * 新闻
	 */
	public function newsIndex()
	{
		$result = Db::name('news')->order("id desc")->paginate(8);
		// $result  = Db::name('news')->select()->toArray();
		$this->assign('page',$result->render());
		$this->assign('result',$result);

		return $this->fetch();
	}

	/**
	 * 新闻添加
	 */
	public function newsAdd()
	{
		return $this->fetch();
	}

	public function newsAddPost()
	{
		$post = $this->request->param();$data = $post['post'];
		//文件上传
		$file = request()->file('img');
		if($file){
	        $info = $file->move(STATIC_ROOT .  'effect/upload_xiaoguotu');
	        if($info){    
	            $file_name =  $info->getSaveName();              
	           
	            $data['img']   = $file_name;		
	           			
	        }else{
	            // 上传失败获取错误信息
	            $this->success("添加失败");
	        }
    	}

    	$data['add_time']    = date('Y-m-d H:i:s');
		$data['desc'] = isset($data['desc'])?html_entity_decode(htmlspecialchars_decode($data['desc'])):'';
		Db::name('news')->insert($data);
		$this->success("添加成功！", url("case/newsIndex"));		
	}

	/**
	 * 删除
	 */
	public function newDelete()
	{
		$id = $this->request->param('id');
		Db::name('news')->where(['id'=>$id])->delete();
		$this->success("删除成功!", url("case/newsIndex"));
	}

	/**
	 * 新闻修改
	 */
	public function newsEdit()
	{
		$id = $this->request->param('id');

		$data = Db::name('news')->where(['id'=>$id])->find();
		$this->assign('data',$data);
		$this->assign('id',$id);
		return $this->fetch();
	}

	/**
	 * 新闻修改
	 */
	public function newsEditPost()
	{
		$post = $this->request->param();
		$data = $post['post'];
		$file = request()->file('img');
		if($file){

	        $info = $file->move(STATIC_ROOT .  'effect/upload_xiaoguotu');
	        if($info){    
	        	//删除以前的照片
	        	$list = Db::name('news')->where(['id'=>$data['id']])->find();
	        	$before_img_path = STATIC_ROOT .  'effect/upload_xiaoguotu/'.$list['img'];
	        	if(is_file($before_img_path))
	        	{
	        		unlink($before_img_path);
	        	}
	        	
	            $file_name =  $info->getSaveName(); 
	            $data['img'] = $file_name;                         			
	        }else{
	            // 上传失败获取错误信息
	            $this->success("修改失败");
	        }
    	}

		$id = $data['id']; unset($data['id']);
		$data['desc'] = isset($data['desc'])?html_entity_decode(htmlspecialchars_decode($data['desc'])):'';

		Db::name('news')->where(['id'=>$id])->update($data);
		$this->success("修改成功!", url("case/newsIndex"));

	}

	/**
	 * 招聘
	 */
	public function zhaopinIndex()
	{
		$result = Db::name('zhaopin')->order("id desc")->paginate(8);
		$this->assign('page',$result->render());
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 招聘添加
	 */
	public function zhaopinAdd()
	{
		return $this->fetch();
	}

	public function zhaopinAddPost()
	{
		$post = $this->request->param();
		$data = $post['post'];
		$data['add_time']    = date('Y-m-d H:i:s');
		$data['desc'] = isset($data['desc'])?html_entity_decode(htmlspecialchars_decode($data['desc'])):'';
		Db::name('zhaopin')->insert($data);
		$this->success("添加成功！", url("case/zhaopinIndex"));
	}

	public function zhaopinEdit()
	{
		$id = $this->request->param('id');

		$data = Db::name('zhaopin')->where(['id'=>$id])->find();
		$this->assign('data',$data);
		$this->assign('id',$id);
		return $this->fetch();
	}

	public function zhaopinEditPost()
	{
		$post = $this->request->param();
		$data = $post['post'];

		$id = $data['id']; unset($data['id']);
		$data['desc'] = isset($data['desc'])?html_entity_decode(htmlspecialchars_decode($data['desc'])):'';

		Db::name('zhaopin')->where(['id'=>$id])->update($data);
		$this->success("修改成功!", url("case/zhaopinIndex"));

	}

	/**
	 * 删除
	 */
	public function zhaopinDelete()
	{
		$id = $this->request->param('id');
		Db::name('zhaopin')->where(['id'=>$id])->delete();
		$this->success("删除成功!", url("case/zhaopinIndex"));
	}

	/**
	 * 留言管理
	 */
	public function liuyanIndex()
	{
		$result = Db::name('liuyan')->order("id desc")->paginate(12);
		$this->assign('page',$result->render());
		$this->assign('result',$result);
		return $this->fetch();
	}
}
